package com.by.zhdoctor.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.by.zhdoctor.domain.ChuFang;

import com.by.zhdoctor.repository.ChuFangRepository;
import com.by.zhdoctor.web.rest.util.HeaderUtil;
import com.by.zhdoctor.web.rest.util.PaginationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing ChuFang.
 */
@RestController
@RequestMapping("/api")
public class ChuFangResource {

    private final Logger log = LoggerFactory.getLogger(ChuFangResource.class);

    @Inject
    private ChuFangRepository chuFangRepository;

    /**
     * POST  /chu-fangs : Create a new chuFang.
     *
     * @param chuFang the chuFang to create
     * @return the ResponseEntity with status 201 (Created) and with body the new chuFang, or with status 400 (Bad Request) if the chuFang has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/chu-fangs")
    @Timed
    public ResponseEntity<ChuFang> createChuFang(@RequestBody ChuFang chuFang) throws URISyntaxException {
        log.debug("REST request to save ChuFang : {}", chuFang);
        if (chuFang.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("chuFang", "idexists", "A new chuFang cannot already have an ID")).body(null);
        }
        ChuFang result = chuFangRepository.save(chuFang);
        return ResponseEntity.created(new URI("/api/chu-fangs/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("chuFang", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /chu-fangs : Updates an existing chuFang.
     *
     * @param chuFang the chuFang to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated chuFang,
     * or with status 400 (Bad Request) if the chuFang is not valid,
     * or with status 500 (Internal Server Error) if the chuFang couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/chu-fangs")
    @Timed
    public ResponseEntity<ChuFang> updateChuFang(@RequestBody ChuFang chuFang) throws URISyntaxException {
        log.debug("REST request to update ChuFang : {}", chuFang);
        if (chuFang.getId() == null) {
            return createChuFang(chuFang);
        }
        ChuFang result = chuFangRepository.save(chuFang);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("chuFang", chuFang.getId().toString()))
            .body(result);
    }

    /**
     * GET  /chu-fangs : get all the chuFangs.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of chuFangs in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/chu-fangs")
    @Timed
    public ResponseEntity<List<ChuFang>> getAllChuFangs(Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of ChuFangs");
        Page<ChuFang> page = chuFangRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/chu-fangs");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /chu-fangs/:id : get the "id" chuFang.
     *
     * @param id the id of the chuFang to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the chuFang, or with status 404 (Not Found)
     */
    @GetMapping("/chu-fangs/{id}")
    @Timed
    public ResponseEntity<ChuFang> getChuFang(@PathVariable Long id) {
        log.debug("REST request to get ChuFang : {}", id);
        ChuFang chuFang = chuFangRepository.findOne(id);
        return Optional.ofNullable(chuFang)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /chu-fangs/:id : delete the "id" chuFang.
     *
     * @param id the id of the chuFang to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/chu-fangs/{id}")
    @Timed
    public ResponseEntity<Void> deleteChuFang(@PathVariable Long id) {
        log.debug("REST request to delete ChuFang : {}", id);
        chuFangRepository.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("chuFang", id.toString())).build();
    }
    /**
     * GET  /chu-fang-zx/:zx : get the "id" chuFang.
     *
     * @param zx the id of the chuFang to retrieve
     * @param bm the id of the chuFang to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the chuFang, or with status 404 (Not Found)
     */
    @GetMapping("/chu-fangs-zx/{zx}/{bm}")
    @Timed
    public ResponseEntity<ChuFang> getChuFangbyzxName(@PathVariable String zx,@PathVariable String bm) {
        log.debug("REST request to get ChuFang : {}", zx);
        ChuFang chuFang = chuFangRepository.findOneByZhengxing_zxNameAndZhengxingBingmingBmName(zx,bm);
        return Optional.ofNullable(chuFang)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }
}
